package com.tjl.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjl.warehouse.common.ErrorCode;
import com.tjl.warehouse.exception.BusinessException;
import com.tjl.warehouse.exception.ThrowUtils;
import com.tjl.warehouse.mapper.UserInfoMapper;
import com.tjl.warehouse.mapper.UserRoleMapper;
import com.tjl.warehouse.model.MyPage;
import com.tjl.warehouse.model.Role;
import com.tjl.warehouse.model.UserInfo;
import com.tjl.warehouse.model.UserRole;
import com.tjl.warehouse.model.dto.RoleDto;
import com.tjl.warehouse.service.RoleService;
import com.tjl.warehouse.mapper.RoleMapper;
import com.tjl.warehouse.service.UserInfoService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Tang
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2024-05-29 20:23:32
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Resource
    private RoleMapper roleMapper;
    @Resource
    UserRoleMapper userRoleMapper;
    @Resource
    UserInfoMapper userInfoMapper;



    @Override
    public List<Role> getRoleList() {
        var query = new LambdaQueryWrapper<Role>();
        //状态为启用的角色
        query.eq(Role::getRoleState,"1");
        return roleMapper.selectList(query);
    }

    @Override
    public MyPage<Role> getRolePage(RoleDto roleDto, MyPage<Role> myPage) {
        var query = new LambdaQueryWrapper<Role>();
        //构造查询条件
        query.like(StringUtils.isNotBlank(roleDto.getRoleName()),
                        Role::getRoleName,roleDto.getRoleName())
                //角色编码
             .like(StringUtils.isNotBlank(roleDto.getRoleCode()),
                        Role::getRoleCode,roleDto.getRoleCode())
                //状态
             .eq(StringUtils.isNotBlank(roleDto.getRoleState()),
                     Role::getRoleState,roleDto.getRoleState());

        IPage<Role> result = roleMapper.selectPage(myPage.toPage(), query);
        return new MyPage<>(result);
    }

    @Override
    public List<Role> getUserRoleById(Integer id) {
        if (id == null || id < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        //查询该用户下的角色
        var userRoleQuery = new LambdaQueryWrapper<UserRole>();
        userRoleQuery.eq(UserRole::getUserId,id);
        //roleId 列表
        List<Integer> roleIds = userRoleMapper.selectList(userRoleQuery)
                .stream().map(UserRole::getRoleId).toList();

        if (roleIds.isEmpty()){
            return new ArrayList<>();
        }

        var roleQuery = new LambdaQueryWrapper<Role>();
        //状态为启用的角色
        roleQuery.eq(Role::getRoleState,"1");
        roleQuery.in(Role::getRoleId,roleIds);
        return roleMapper.selectList(roleQuery);
    }

    @Override
    public boolean addRole(Role role) {
        return false;
    }

    @Override
    public boolean deleteAllRole(Integer userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        ThrowUtils.throwIf(userInfo == null,ErrorCode.PARAMS_ERROR,"用户不存在！");

        var query = new LambdaQueryWrapper<UserRole>();
        query.eq(UserRole::getUserId,userId);
        int delete = userRoleMapper.delete(query);

        return delete > 0;
    }

    @Override
    public boolean updateRole(Role role) {
        ThrowUtils.throwIfParams(role == null);
        ThrowUtils.throwIf(role.getRoleId() <= 0,ErrorCode.PARAMS_ERROR,"id为空");
        int i = roleMapper.updateById(role);

        return i > 0;
    }
}




